package com.qf.openapi.wxpay.utils;

import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayConfig;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 基于SDK封装微信支付的 获取支付url和查询订单状态接口
 */
public class WxPayUtils {

    public static void main(String[] args) throws Exception {
        String oid = "202307131107999";
        String productdesc = "xiaomishouji";
        String ammout = "3000";

        String appid = "wx632c8f211f8122c6";
        String mch_id = "1497984412";
        String appkey = "sbNCm1JnevqI36LrEaxFwcaT0hkGxFnC";

        WXPayConfig config = new WXPayConfig() {
            @Override
            public String getAppID() {
                return appid;
            }

            @Override
            public String getMchID() {
                return mch_id;
            }

            @Override
            public String getKey() {
                return appkey;
            }

            @Override
            public InputStream getCertStream() {
                return null;
            }

            @Override
            public int getHttpConnectTimeoutMs() {
                return 0;
            }

            @Override
            public int getHttpReadTimeoutMs() {
                return 0;
            }
        };

//        String url = WxPayUtils.getpayUrl(oid, productdesc, ammout, config);
//        System.out.println(url);

        String status = WxPayUtils.queryOrderStatus(oid,config);
        System.out.println(status);
    }

    public static String queryOrderStatus(String oid,WXPayConfig wxPayConfig) throws Exception {

        WXPay wxPay = new WXPay(wxPayConfig);

        HashMap<String, String> data = new HashMap<>();
        data.put("out_trade_no",oid);

        Map<String, String> resultmap = wxPay.orderQuery(data);
        return resultmap.get("trade_state");

    }


    public static String getpayUrl(String oid, String productdesc, String amount, WXPayConfig wxPayConfig) throws Exception {

        WXPay wxPay = new WXPay(wxPayConfig);
        // 组织参数
        HashMap<String, String> data = new HashMap<>();
        data.put("body",productdesc);  //商品描述
        data.put("out_trade_no",oid);               //使用当前用户订单的编号作为当前支付交易的交易号
        data.put("fee_type","CNY");                     //支付币种
        data.put("total_fee","1");
        data.put("trade_type","NATIVE");                //交易类型
        data.put("notify_url","http://47.118.45.73:8080/pay/callback");           //设置支付完成时的回调方法接口

        Map<String, String> resultmap = wxPay.unifiedOrder(data);

        String codeUrl = resultmap.get("code_url");

        return codeUrl;

    }

}
